<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/3  15:51
 */
namespace app\admin\controller;
use app\api\controller\Pay;
use think\Db;

class Order extends Common
{
    private $table_admin = "";
    /*
   *	构造函数
   */
    function __construct()
    {
        parent::__construct();
        $this->table_admin=db('order');
    }

    public function index()
    {
        $pageParam = ['query'=>[]];
        $where = [];
        $time = input('time');
        if($time){
            $timearr = explode(' - ', $time);
            $start_time = strtotime($timearr[0]);
            $end_time = strtotime($timearr[1]);
            $where['create_time'] = ['between',[$start_time,$end_time]];
            $this->assign('time', $time);
            $pageParam['query']['time'] = $time;
        }

        $status = input('status');
        if($status!=''){
            $where['status'] = $status;
            $pageParam['query']['status'] = $status;
        }
        $this->assign('status', $status);

        $order_sn = input('order_sn');
        if($order_sn){
            $where['order_sn'] = ['like', '%'.$order_sn.'%'];
            $pageParam['query']['order_sn'] = $order_sn;
            $this->assign('order_sn', $order_sn);
        }

        $data = $this->table_admin->where($where)
            ->field('id,order_sn,product_name,user_id,mobile,address,total_count,total_price,status,create_time')
            ->order('id desc')
            ->paginate(15)->each(function($item,$key){
            $user = db('member')->where('id='.$item['user_id'])->value('nickname');
            $item['total_price'] = $item['total_price']/100;
            $item['user'] = $user;
            return $item;
        });
//        halt($data);
        $order_status = [
            '0'=>'代付款',
            '1'=>'已付款',
            '2'=>'处理中',
            '3'=>'已完成',
            '-2'=>'已关闭',
        ];

        $this->assign('order_status', $order_status);
        $this->assign('page', $data->render());
        $this->assign('data', $data);
        return $this->fetch();
    }

    //查看订单详情
    public function detail()
    {
        $id = input('id/d');//订单id
        $user = input('user');
        if(!$id)return $this->error('参数有误');
        $data = $this->table_admin->where('id='.$id)->find();
        if(!$user && $data['user_id']){
            $user = db('member')->where('id='.$data['user_id'])->value('nickname');

        }
        $data['user'] = $user;
        $data['gd'] = db('order_goods')
            ->where('order_id='.$id)
            ->field('id,goods_name,goods_num,goods_price,circle_name')
            ->order('id desc')
            ->select();
        foreach($data['gd'] as $k=>$val){
            $data['gd'][$k]['goods_price'] = $data['gd'][$k]['goods_price']/100;
        }
        $data['total_price'] = $data['total_price']/100;
//        halt($data);
        $this->assign('da', $data);
        return $this->fetch();
    }

    //退货列表
    public function refundList()
    {
        $order_return = db('order_return')->order('id desc')->paginate(15)->each(function($item, $key){
            if(!empty($item['certificate_image'])){
                $item['certificate_image'] = json_decode($item['certificate_image'],1);
                foreach ($item['certificate_image'] as $key => &$value) {
                    $value = IMG_PATH.$value; //给数据集追加字段num并赋值
                }
            }else{
                $item['certificate_image'] = [];
            }
            $item['goods_price'] = $item['goods_price']/100;
            return $item;
        });
        $this->assign('list',$order_return);
        $this->assign('page',$order_return->render());
        return $this->fetch();
    }

    //退货查看
    public function refunddetail()
    {
        $id = input('id/d');//订单id
        $order_return = db('order_return')->where(['id'=>$id])->find();
        $order_return['certificate_image'] = json_decode($order_return['certificate_image'],1);
        $user_id = db('order')->where(['id'=>$order_return['order_id']])->field('user_id,mobile')->find();

        //查询
        $user_member = db('member')->where(['id'=>['in',[$user_id['user_id'], $order_return['buyer_user_id']]]])->field('nickname,headimage,id,wx_openid')->select();

        foreach ($user_member as $key => $value) {
            //防止自己买自己商铺的东西出BUG
            if($order_return['buyer_user_id'] != $user_id['user_id']){
                if($value['id'] == $user_id['user_id']){
                    $order_return['user_nickname'] = $value['nickname'];
                    $order_return['user_headimage'] = $value['headimage'];
                    $order_return['user_mobile'] = $user_id['mobile'];
                    $order_return['user_wx_openid'] = $value['wx_openid'];
                }

                if($value['id'] == $order_return['buyer_user_id']){
                    $order_return['buy_user_nickname'] = $value['nickname'];
                    $order_return['buy_user_wx_openid'] = $value['wx_openid'];
                }
            }else{
                $order_return['user_nickname'] = $value['nickname'];
                $order_return['user_headimage'] = $value['headimage'];
                $order_return['user_mobile'] = $user_id['mobile'];
                $order_return['user_wx_openid'] = $value['wx_openid'];
                $order_return['buy_user_nickname'] = $value['nickname'];
                $order_return['buy_user_headimage'] = $value['headimage'];
                $order_return['buy_user_wx_openid'] = $value['wx_openid'];
            }
        }
        
        $this->assign('order_return',$order_return);
        return $this->fetch();
    }

    //确认退货金额
    public function refunedit(){
        if(request()->isAjax()){
            $id = input('id/d');//订单id
            //$goodsid = input('goodsid/d');//订单id

            $order_return = db('order_return')->where(['id'=>$id])->order('id desc')->find();

            if($order_return['status'] != '1')return $this->error('该订单未审核或者不允许退货！');

            $result = (new Pay($this->request))->wechat_refund($order_return['order_id'],$order_return['refund_sn']);
            if(!$result) $this->success('退款失败!');

            db('order_return')->where(['refund_sn' => $order_return['refund_sn']])->update(['money_arrives_status' => '1']);
            
            $data = [];
            $data['type'] = '-1';
            $data['type_name'] = '3';
            $data['price'] = $order_return['goods_price'];
            $data['order_return_id'] = $order_return['id'];

            db('platform_amount')->insert($data);
            $this->success('操作成功!',url('order/refundlist'));
        }
    }

}
